home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / TOOLS_&_ / BUTTONEE / MAIN.C next >
C/C++ Source or Header  |  1991-02-22  |  3KB  |  130 lines

  1. /* Buttoneer demo source code. */
  2.  
  3. /* Used to outline the default button (usually "OK") in a dialog */
  4. static void OutlineButton(DialogPtr dialog, short itemNo)
  5. {    short itemType;
  6.     ControlHandle item;
  7.     Rect box;
  8.  
  9.     SetPort(dialog);
  10.     GetDItem(dialog, itemNo, &itemType, (Handle *)&item, &box);
  11.     PenSize(3, 3);
  12.     InsetRect(&box, -4, -4);
  13.     FrameRoundRect(&box, 16, 16);
  14.     PenSize(1, 1);
  15. }
  16.  
  17. static short FlipDialogCheckBox(DialogPtr dialog, short itemNo)
  18. {    short itemType;
  19.     ControlHandle item;
  20.     Rect box;
  21.     short previousValue;
  22.  
  23.     GetDItem(dialog, itemNo, &itemType, (Handle *)&item, &box);
  24.     SetCtlValue(item, !(previousValue = GetCtlValue(item)));
  25.     return previousValue;
  26. }
  27.  
  28. /* This is a standard dialog filter function.
  29.  * It will treat RETURN and ENTER as item 1, and outline item 1 as a button.
  30.  * Additional features may be added.
  31.  */
  32. static pascal Boolean STDfilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  33. {    short i;
  34.  
  35.     switch(theEvent->what)
  36.     {
  37.         case keyDown:
  38.         {    i = theEvent->message & 255;
  39.             if ((i == 3) || (i == 13))    /* Return or Enter */
  40.             {    *itemHit = 1;
  41.                 return 1;
  42.             }
  43.             break;
  44.         }
  45.         /* Handling of updateEvents this way was suggested and implemented
  46.          * by Clayton Prestia.
  47.          */
  48.         case updateEvt:
  49.         {    theEvent->what = nullEvent; /* Keep the DLOG mngr's fingers out of the pie */
  50.             BeginUpdate(theDialog);
  51.             SetPort(theDialog);
  52.             EraseRect(&theDialog->portRect);
  53.             UpdtDialog(theDialog,theDialog->visRgn);
  54.             OutlineButton(theDialog, 1);
  55.             EndUpdate(theDialog);
  56.             break;
  57.         }
  58.     }
  59.     return FALSE;
  60. }
  61.  
  62. /* Call this to show a dialog window that has an 'OK' default.
  63.  * It will first check to see if the return or enter key has already
  64.  * been pressed, and skip the dialog.  This saves time on redisplays.
  65.  */
  66. static short MaybeShowWindow(DialogPtr dPtr)
  67. {    EventRecord myEvent;
  68.     short x;
  69.  
  70.     if(EventAvail(everyEvent, &myEvent))
  71.     {    if(myEvent.what == keyDown)
  72.         {    x = myEvent.message & charCodeMask;
  73.             if(x == 3 || x == 13)    /* Return or Enter. */
  74.             {    GetNextEvent(everyEvent, &myEvent);
  75.                 return 1;
  76.             }
  77.         }
  78.     }
  79.     ShowWindow(dPtr);
  80.     SetPort(dPtr);
  81.     return 0;
  82. }
  83.  
  84. static void TestDialog(void)
  85. {    DialogPtr    dPtr;
  86.     short i;
  87.      short done = FALSE;
  88.  
  89.     dPtr = GetNewDialog(128, NULL, (WindowPtr)-1);
  90.  
  91.     /* Setup Dialog Here */
  92.     
  93.     /* Now make it visible and do it. */
  94.     if(MaybeShowWindow(dPtr))goto ok;
  95.  
  96.     while(!done)
  97.     {    ModalDialog((ProcPtr)STDfilter, &i);
  98.         switch(i)
  99.         {    case 1:                /* OK */
  100.             ok:
  101.                 /* Read the dialog here */
  102.                 done = TRUE;
  103.                 break;
  104.             case 2:                /* Cancel */
  105.                 done = TRUE;
  106.                 break;
  107.             case 3:
  108.             case 4:
  109.                 FlipDialogCheckBox(dPtr, i);
  110.                 break;
  111.             default:
  112.                 break;
  113.         }
  114.     }
  115.     DisposDialog(dPtr);
  116. }
  117.  
  118. void main(void)
  119. {    InitGraf((Ptr)&thePort);
  120.     InitFonts();
  121.     FlushEvents(everyEvent, 0);
  122.     InitWindows();
  123.     InitMenus();
  124.     TEInit();
  125.     InitDialogs(0L);
  126.     InitCursor();
  127.     
  128.     TestDialog();
  129. }
  130.